//
//  photoCommCell.m
//  PhotoCircle
//
//  Created by wujungao on 12/15/15.
//  Copyright © 2015 wujungao. All rights reserved.
//

#import "photoCommCell.h"
#import "UIImageView+WebCache.h"
#import "SDWebImageManager.h"
#import "GMTipsHelper.h"

#define bottomViewH 49
#define tpH 64

@interface photoCommCell()<UIScrollViewDelegate>

@end

@implementation photoCommCell

-(instancetype)initWithFrame:(CGRect)frame{
    
    self=[super initWithFrame:frame];
    if(self){
        
    }
    
    return self;
}

#pragma mark - pub method
-(void)configCellWithImageURL:(NSString *)imageURL{
    
    NSURL *url=[NSURL URLWithString:imageURL];
    
    if(![[SDWebImageManager sharedManager] cachedImageExistsForURL:url]){
        
        [GMTipsHelper showLoading];
    }
    
    [self.imageView sd_setImageWithURL:url placeholderImage:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
        [GMTipsHelper showDismiss];
        [self configCellWithImage:image];
    }];
}

#pragma mark - config views
-(void)configCellView{
    
    _imageView=nil;
    _scrollView=nil;
    self.backgroundColor=[UIColor blackColor];
    self.userInteractionEnabled=YES;
    
    self.imageView.backgroundColor=[UIColor blackColor];
    self.imageView.userInteractionEnabled=YES;
    
    self.scrollView.backgroundColor=[UIColor blackColor];
    self.scrollView.userInteractionEnabled=YES;
    self.scrollView.showsHorizontalScrollIndicator=NO;
    self.scrollView.showsVerticalScrollIndicator=NO;
}

-(void)configCellWithImage:(UIImage *)image{
    
    [self configImageViewWithImageSize:image.size];
    [self configScrollViewZoomScaleWithImageSize:image.size];
}

-(void)configImageViewWithImageSize:(CGSize)imageSize{
    
    CGSize maxSize=CGSizeMake(SCREEN_W, SCREEN_H);
    CGFloat imageRatio = imageSize.width / imageSize.height;
    CGFloat screenRatio = maxSize.width / maxSize.height;
    
    CGFloat newWidth = (imageRatio > screenRatio) ? maxSize.width : maxSize.height * imageRatio ;
    CGFloat newHeight = (imageRatio > screenRatio) ? maxSize.width / imageRatio : maxSize.height ;
        
    self.imageView.bounds=CGRectMake(0, 0, newWidth, newHeight);
    self.imageView.center=CGPointMake(SCREEN_W/2.0, SCREEN_H/2.0);
}

-(void)configScrollViewZoomScaleWithImageSize:(CGSize)imageSize{
    
    CGSize maxSize=CGSizeMake(SCREEN_W, SCREEN_H);
    
    CGFloat widthRatio = maxSize.width/imageSize.width;
    CGFloat heightRatio = maxSize.height/imageSize.height;
    CGFloat zoom = (widthRatio > heightRatio) ? heightRatio : widthRatio;
    
    CGFloat initialZoom= (zoom > 1.0 && zoom < 2.0) ? zoom : 1.0;
    
    self.scrollView.maximumZoomScale=3.0;
    self.scrollView.minimumZoomScale=initialZoom;
}

#pragma mark - property

-(UIImageView *)imageView{
    
    if(_imageView==nil){
    
        _imageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H)];
        
        [self.scrollView addSubview:_imageView];
    }
    
    return _imageView;
}

-(UIScrollView *)scrollView{
    
    if(_scrollView==nil){
        
        _scrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H)];
        
        [self addSubview:_scrollView];
    }
    
    return _scrollView;
}

@end
